<?php

/**
 * answer actions.
 *
 * @package    question_answer
 * @subpackage answer
 * @author     Your name here
 */
class answerActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {	
	//$this->form = new AddResultForm();	
	$this->form = new resultForm();
	 
  
	$user_id = $this->getUser()->getGuardUser()->getId();
    $this->userss=$user_id;
    
    $q= QuestionPeer::getWithJobs(1,$this->userss);

    if(count($q)>0)	        
    {
    	$this->answers =$q;
    }
    else
    {
	$this->redirect('result/index');
    }   	
	            
   }

 
  public function executeShow(sfWebRequest $request)
  {
    $this->answer = AnswerPeer::retrieveByPk($request->getParameter('id'));
    $this->forward404Unless($this->answer);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new answerForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new answerForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($answer = AnswerPeer::retrieveByPk($request->getParameter('id')), sprintf('Object answer does not exist (%s).', $request->getParameter('id')));
    $this->form = new answerForm($answer);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($answer = AnswerPeer::retrieveByPk($request->getParameter('id')), sprintf('Object answer does not exist (%s).', $request->getParameter('id')));
    $this->form = new answerForm($answer);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($answer = AnswerPeer::retrieveByPk($request->getParameter('id')), sprintf('Object answer does not exist (%s).', $request->getParameter('id')));
    $answer->delete();

    $this->redirect('answer/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $answer = $form->save();

      $this->redirect('answer/edit?id='.$answer->getId());
    }
  }
}
